/**
 * Created by Administrator on 2017/7/28.
 */

/**
 * location.search
 *传入key，返回location.search中对应的value
 *截取出当前url后面的id
 * */
function locationSearcher(key) {
    var search = location.search.split('?');
    if(search.length>1){
        var params = search[1].split('&');
        for(var i=0;i<params.length; i++){
            var item = params[i].split('=');
            var k = item[0];
            if(key == k){
                return item[1];
            }
        }
    }
    return null;
}


/**
 * 小数相加 解决JS小数运算BUG
 * @param num1
 * @param num2
 * @param type 加或者减 'jia'、'jian'
 * @returns {number}
 */
function addFloatNum(num1, num2, type) {
    num1 = parseFloat(num1);
    num2 = parseFloat(num2);
    type = type ? type : 'jia';
    var sq1, sq2, m;
    try {
        sq1 = num1.toString().split(".")[1].length;
    } catch (e) {
        sq1 = 0;
    }
    try {
        sq2 = num2.toString().split(".")[1].length;
    } catch (e) {
        sq2 = 0;
    }
    m = Math.pow(10, Math.max(sq1, sq2));
    return type == 'jia' ? (Math.round(num1 * m) + Math.round(num2 * m)) / m : (Math.round(num1 * m) - Math.round(num2 * m)) / m;
}

/**
 * 小数乘法
 * @param arg1
 * @param arg2
 * @returns {number}
 */
function accMul(arg1,arg2) {
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}

/**
 * 保证数字有两位小数，四舍五入，不够补0
 * @param x
 * @returns {*}
 */
function toDecimal2(x) {
    var f1 = parseFloat(x);
    if (isNaN(f1)) {
        return false;
    }
    var f2 = Math.round(x*100)/100;
    var s = f2.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}


/*模拟弹窗*/
var dialog={};
dialog.alert = function (data) {
    data.type = 'alert';
    data.width = '270px';
    data.title = data.title ? data.title : '温馨提示';
    data.okText = data.okText ? data.okText : "知道了";
    return $.dialog(data);
};

dialog.confirm = function (data) {
    data.type = 'confirm';
    data.width = '270px';
    return $.dialog(data);
};

dialog.loading = function (con) {
    return $.dialog({content: con, titleIcon: "load", width: '230px', type: "loading"});
};

dialog.tusiSuccess = function (con) {
    return $.dialog({content: con, titleIcon: "success", width: '230px', type: "tusi", time: 1000});
};
dialog.optionSuccess = function (con) {
    return $.dialog({content: con, titleIcon: "success", width: '230px', type: "tusi", time: 3000});
};
dialog.tusiError = function (con , time) {
    time = time ? time : 3000;
    return $.dialog({content: con, titleIcon: "error", width: '230px', type: "tusi", time: time});
};

dialog.tusi = function (con) {
    return $.dialog({content: con, width: '230px', type: "tusi", time: 1000});
};


/**
 * 登录过期之后
 * 调用原生重新登录
 *
 * */
function againLogin() {

    var againData={
        postType:'againLogin'

    };

    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
        iosAgainLogin(againData);
    } else {
        androidAgainLogin(JSON.stringify(againData));
    }
}


function iosAgainLogin(param) {
    window.webkit.messageHandlers.againLogin.postMessage(param);
}

function androidAgainLogin(param) {
    window.huifa.againLogin(param);
}



